<template>
  <div class="tags-view-container">
    <hamburger
      :toggle-click="toggleSideBar"
      :is-active="sidebar.opened"
      class="hamburger-container"
    />
    <scroll-pane ref="scrollPane" class="tags-view-wrapper">
      <template v-for="(route, index) in $route.matched">
        <template v-if="index === 0">
          <span class="tags-span" @click="goTo(route.path)" :key="`${route.name}_name`">{{
            route.name
          }}</span>
          <span class="tags-span right" :key="`${route.name}_arrow`">> ></span>
        </template>
        <template v-else>
          <template v-if="route.meta.parentUrl">
            <span class="tags-span" @click="goTo(route.meta.parentUrl)" :key="`${route.meta.name}_name`">{{
              route.meta.name
            }}</span>
            <span class="tags-span right" :key="`${route.meta.name}_arrow`">> ></span>
          </template>
          <span class="tags-span" @click="goTo(route.path)" :key="`${route.name}_name`">{{
            route.meta.title
          }}</span>
          <span class="tags-span right" :key="`${route.name}_arrow`">> ></span>
        </template>
      </template>
      <div class="addTagsList"></div>
    </scroll-pane>
  </div>
</template>

<script>
import ScrollPane from "@/components/ScrollPane";
import { generateTitle } from "@/utils";
import Hamburger from "@/components/Hamburger";
export default {
  components: { ScrollPane, Hamburger },
  data() {
    return {
      visible: true,
      top: 0,
      left: 0,
      selectedTag: {},
    };
  },
  computed: {
    sidebar() {
      return this.$store.getters.sidebar;
    },
    visitedViews() {
      return {
        parent: this.$route.matched[0].name,
        parentName: this.$route.meta.name,
        name: this.$route.meta.title,
        path: this.$route.path
      };
    },
  },
  watch: {
    $route() {
      this.addViewTags();
      this.moveToCurrentTag();
    },
    visible(value) {
      if (value) {
        document.body.addEventListener("click", this.closeMenu);
      } else {
        document.body.removeEventListener("click", this.closeMenu);
      }
    },
  },
  mounted() {
    this.addViewTags();
  },
  methods: {
    goTo(data) {
      if (this.$route.path !== data) {
        this.$router.push(data);
      }
    },
    toggleSideBar() {
      const store = this.$store;
      this.$store.dispatch("toggleSideBar").then(function () {
        Object.keys(store.state.app.chartListener).forEach((key) => {
          store.state.app.chartListener[key].resize();
        });
      });
    },
    generateTitle,
    isActive(route) {
      return route.path === this.$route.path;
    },
    addViewTags() {
      const { name } = this.$route;
      if (name) {
        this.$store.dispatch("addView", this.$route);
      }
      return false;
    },
    moveToCurrentTag() {
      const tags = this.$refs.tag;
      this.$nextTick(() => {
        if (tags) {
          for (const tag of tags) {
            if (tag.to.path === this.$route.path) {
              this.$refs.scrollPane.moveToTarget(tag);
              if (tag.to.fullPath !== this.$route.fullPath) {
                this.$store.dispatch("updateVisitedView", this.$route);
              }
              break;
            }
          }
        }
      });
    },
    refreshSelectedTag(view) {
      this.$store.dispatch("delCachedView", view).then(() => {
        const { fullPath } = view;
        this.$nextTick(() => {
          this.$router.replace({
            path: "/redirect" + fullPath,
          });
        });
      });
    },
    closeSelectedTag(view) {
      this.$store.dispatch("delView", view).then(({ visitedViews }) => {
        if (this.isActive(view)) {
          const latestView = visitedViews.slice(-1)[0];
          if (latestView) {
            this.$router.push(latestView);
          } else {
            this.$router.push("/");
          }
        }
      });
    },
    closeOthersTags() {
      this.$router.push(this.selectedTag);
      this.$store.dispatch("delOthersViews", this.selectedTag).then(() => {
        this.moveToCurrentTag();
      });
    },
    closeAllTags() {
      this.$store.dispatch("delAllViews");
      this.$router.push("/");
    },
    openMenu(tag, e) {
      const menuMinWidth = 105;
      const offsetLeft = this.$el.getBoundingClientRect().left;
      const offsetWidth = this.$el.offsetWidth;
      const maxLeft = offsetWidth - menuMinWidth;
      const left = e.clientX - offsetLeft + 15;

      if (left > maxLeft) {
        this.left = maxLeft;
      } else {
        this.left = left;
      }
      this.top = e.clientY;

      this.visible = true;
      this.selectedTag = tag;
    },
    closeMenu() {
      this.visible = false;
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.hamburger-container {
  width: 25px;
  float: left;
  text-align: center;
  vertical-align: middle;
  height: 34px;
}
.scroll-container {
  width: calc(100% - 46px);
  float: left;
}
.tags-view-container {
  height: 34px;
  width: calc(100% - 20px);
  background: #fff;
  padding: 0 10px;
  border-bottom: 5px solid #eee;
  .tags-view-wrapper {
    margin-top: 2px;
    .tags-view-item {
      display: inline-block;
      position: relative;
      cursor: pointer;
      height: 26px;
      line-height: 26px;
      border: 1px solid #d8dce5;
      color: #495060;
      background: #fff;
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 5px;
      &:first-of-type {
        margin-left: 15px;
      }
      &:last-of-type {
        margin-right: 15px;
      }
      &:hover {
        background-color: #409eff;
        border-color: #409eff;
        transition: 0.3s ease-in;
        color: #fff;
      }
      &.active {
        background-color: rgba(45, 142, 240, 1);
        color: #fff;
        border-color: rgba(45, 142, 240, 1);
        &::before {
          content: "";
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 2px;
        }
      }
    }
    .tags-span {
      display: inline-block;
      // height: 100%;
      line-height: 33px;
      margin: 0 10px;
      color: #c0c4cc;
      vertical-align: middle;
      cursor: pointer;
      font-size: 16px;
    }
    .right {
      margin: 0;
      font-weight: 700;
      color: #c0c4cc;
    }
  }
  // .tags-view-wrapper {
  //   .tags-view-item {
  //     display: inline-block;
  //     position: relative;
  //     cursor: pointer;
  //     height: 26px;
  //     line-height: 26px;
  //     border: 1px solid #d8dce5;
  //     color: #495060;
  //     background: #fff;
  //     padding: 0 8px;
  //     font-size: 12px;
  //     margin-left: 5px;
  //     margin-top: 5px;
  //     &:first-of-type {
  //       margin-left: 15px;
  //     }
  //     &:last-of-type {
  //       margin-right: 15px;
  //     }
  //     &:hover
  //     {
  //       background-color: #409EFF;
  //       border-color: #409EFF;
  //       transition: 0.3s ease-in;
  //       color: #fff;
  //     }
  //     &.active {
  //       background-color: #29a7e1;
  //       color: #fff;
  //       border-color: #29a7e1;
  //       &::before {
  //         content: '';
  //         background: #fff;
  //         display: inline-block;
  //         width: 8px;
  //         height: 8px;
  //         border-radius: 50%;
  //         position: relative;
  //         margin-right: 2px;
  //       }
  //     }
  //   }
  // }
  .contextmenu {
    margin: 0;
    background: #fff;
    z-index: 100;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;
      &:hover {
        background: #eee;
      }
    }
  }
}
</style>

<style rel="stylesheet/scss" lang="scss">
.tags-view-wrapper {
  .tags-view-item {
    .el-icon-close {
      width: 16px;
      height: 16px;
      vertical-align: 2px;
      border-radius: 50%;
      text-align: center;
      transform-origin: 100% 50%;
      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
      }
    }
  }
}
</style>
